(function(angular) {
	"use strict";
	angular.module("fs.waf").controller("cls.popup.txnFeeList.listController", listController);
	listController.$inject = [ "$scope", "dialogsModule", "$uibModalInstance", "gridService", "modalService", "layoutConfig", "validatorConfig", "authService" ];
	function listController($scope, dialogsModule, $uibModalInstance, gridService, modalService, layoutConfig, validatorConfig, authService) {
		var ctrl = this;
		ctrl.dialogsModule = dialogsModule;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.searchCriteria = ctrl.searchCriteria || {};

		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback["close"] || dialogsModule.btnCallback["close"]() !== false) {
				$uibModalInstance.dismiss();
			}
		};

		ctrl.init = function() {
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.tableConfigs.table, rowSelectionChanged);
			ctrl.innerGridOptions.data = "vm.searchCriteria.list";
			ctrl.innerGridOptions.multiSelect = false;
			ctrl.feeStatusOptions = [{
				"value" : "C",
				"label" : "option.label.created"
			},{
				"value" : "W",
				"label" : "option.label.waived"
			},{
				"value" : "P",
				"label" : "option.label.paid"
			},{
				"value" : "F",
				"label" : "option.label.finished"
			},{
				"value" : "O",
				"label" : "option.label.carried-over"
			},{
				"value" : "T",
				"label" : "option.label.transfered"
			}];
			if (!ctrl.dialogsModule.params.txnId) {
				return;
			}
			ctrl.searchCriteria = angular.merge(ctrl.searchCriteria, ctrl.dialogsModule.params);
			ctrl.search();
		};

		ctrl.feeAmortize = function() {
			if (!ctrl.selectedRows || ctrl.selectedRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ "title.dialogs.info.norecsel" ]
				});
				return;
			}
			modalService.openCustomDialogs({
				"title" : "title.popup.list.feeamortize",
				"dialogsModule" : "cls.loan.feeAmortizeList",
				"size" : [ 800, 300 ],
				"params" : ctrl.selectedRows[0],
				"btnCallback" : {
					"ok" : function() {
					}
				}
			});
		};

		ctrl.viewTxnFee = function(currRow) {
			modalService.openCustomDialogs({
				"title" : "title.popup.view.txnfee",
				"dialogsModule" : "cls.loan.txnFeeView",
				"params" : currRow.entity,
				"btnCallback" : {
					"ok" : function() {
					}
				}
			});
		};

		ctrl.search = function() {
			ctrl.searchCriteria.list = [];
			authService.search(ctrl.searchCriteria).then(function(response) {
				angular.merge(ctrl.searchCriteria, response.data);
				gridService.updateGridOptions(ctrl.searchCriteria, ctrl.innerGridOptions);
			}, angular.noop);
		};

		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.selectedRows = selectedRows;
		}

	}
})(angular);